package com.ruoyi.common.utils.crudinterface;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.filter.NameFilter;
import com.ruoyi.common.config.DbInterfaceConfig;
import com.ruoyi.common.utils.crudinterface.vo.ReqVo;
import com.ruoyi.common.utils.crudinterface.vo.ResVo;
import com.ruoyi.common.utils.http.Http2Utils;


/**
 * 调用接口操作增删改查.
 *
 * @author :yanlixiong
 * @date :2025-02-19 13:37:18
 */

public class CrudInterfaceUtils {

    /**
     * 获取单个对象.
     *
     * @return the bean
     * @author :yanlixiong
     * @date :2025-02-19 13:37:57
     */
    public Object getBean(DbInterfaceConfig config, ReqVo reqVo, Class clazz) {
        getNewReqVo(reqVo);
        String result = Http2Utils.doPost(config.getBaseUrl()+config.getGetBean(), JSON.toJSONString(reqVo));
        ResVo resVo =  JSON.parseObject(result,ResVo.class);
        String newResult =  this.handleCamelName((JSONObject)resVo.getData());
        Object o = JSON.parseObject(newResult, clazz);
        return o;
    }

    /**
     * 获取List.
     *
     * @return the bean
     * @author :yanlixiong
     * @date :2025-02-19 13:37:57
     */
    public Object getList(DbInterfaceConfig config,ReqVo reqVo, Class clazz) {
        getNewReqVo(reqVo);
        String result = Http2Utils.doPost(config.getBaseUrl()+config.getGetList(), JSON.toJSONString(reqVo));
        ResVo resVo =  JSON.parseObject(result,ResVo.class);
        String newResult =  this.handleCamelName2((JSONArray)resVo.getData());
        Object o = JSON.parseArray(newResult, clazz);
        return o;
    }

    /**
     * 创建.
     *
     * @return the bean
     * @author :yanlixiong
     * @date :2025-02-19 13:37:57
     */
    public Object create(DbInterfaceConfig config,ReqVo reqVo) {
        getNewReqVo(reqVo);
        String result = Http2Utils.doPost(config.getBaseUrl()+config.getCreate(), JSON.toJSONString(reqVo));
        ResVo resVo = JSON.parseObject(result,ResVo.class);
        return resVo.getData();
    }

    /**
     * 更新.
     *
     * @return the bean
     * @author :yanlixiong
     * @date :2025-02-19 13:37:57
     */
    public Object update(DbInterfaceConfig config,ReqVo reqVo) {
        getNewReqVo(reqVo);
        String result = Http2Utils.doPost(config.getBaseUrl()+config.getUpdate(), JSON.toJSONString(reqVo));
        ResVo resVo = JSON.parseObject(result,ResVo.class);
        return resVo.getData();
    }

    /**
     * 删除.
     *
     * @return the bean
     * @author :yanlixiong
     * @date :2025-02-19 13:37:57
     */
    public Object delete(DbInterfaceConfig config,ReqVo reqVo) {
        getNewReqVo(reqVo);
        String result = Http2Utils.doPost(config.getBaseUrl()+config.getDelete(), JSON.toJSONString(reqVo));
        ResVo resVo = JSON.parseObject(result,ResVo.class);
        return resVo.getData();
    }

    private void getNewReqVo(ReqVo reqVo){
        //增加其他参数：比如token、userId
        //reqVo.set
    }

    /**
     * 使用 NameFilter 实现驼峰命名转换.
     *
     * @return the string
     * @author :yanlixiong
     * @date :2025-02-21 09:39:59
     */
    private String handleCamelName(JSONObject jsonObject){
        String camelCaseJsonStr = JSON.toJSONString(jsonObject, (NameFilter) (object, name, value) -> {
            // 驼峰转换
            if (name.contains("_")) {
                StringBuilder sb = new StringBuilder();
                for (String part : name.split("_")) {
                    if (sb.length() == 0) {
                        sb.append(part.toLowerCase());
                    } else {
                        sb.append(part.substring(0, 1).toUpperCase()).append(part.substring(1).toLowerCase());
                    }
                }
                return sb.toString();
            }
            return name;
        });
        return camelCaseJsonStr;
    }

    /**
     * 使用 NameFilter 实现驼峰命名转换.
     *
     * @return the string
     * @author :yanlixiong
     * @date :2025-02-21 09:39:59
     */
    private String handleCamelName2(JSONArray jsonArray){
        String camelCaseJsonStr = JSON.toJSONString(jsonArray, (NameFilter) (object, name, value) -> {
            // 驼峰转换
            if (name.contains("_")) {
                StringBuilder sb = new StringBuilder();
                for (String part : name.split("_")) {
                    if (sb.length() == 0) {
                        sb.append(part.toLowerCase());
                    } else {
                        sb.append(part.substring(0, 1).toUpperCase()).append(part.substring(1).toLowerCase());
                    }
                }
                return sb.toString();
            }
            return name;
        });
        return camelCaseJsonStr;
    }


}

